package org.daidai.abstrusewriting.homepage.classify.activitys;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.daidai.abstrusewriting.R;
import org.daidai.abstrusewriting.homepage.classify.adapter.TrackAdapter;
import org.daidai.abstrusewriting.homepage.classify.client.ClientAPI;
import org.daidai.abstrusewriting.homepage.classify.model.Album;
import org.daidai.abstrusewriting.homepage.classify.model.Special;
import org.daidai.abstrusewriting.homepage.classify.model.SpecialDetail;
import org.daidai.abstrusewriting.homepage.classify.model.Track;
import org.daidai.abstrusewriting.homepage.top.PlayActivity;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

public class SpecialDetailActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener, AbsListView.OnScrollListener, AdapterView.OnItemClickListener {
    private ImageView mImgCover;
    private TextView mTitleTxt;
    private TextView mPlayTxt;
    private TextView mPrecenterTxt;
    private TextView mClassifyTxt;
    private List<Track> mList;
    private TrackAdapter trackAdapter;
    private ListView mListView;
    private SwipeRefreshLayout mRefresh;
    private Special special;
    private int pageId=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_special_detail);
        translucentStatusBar();
        if (!(EventBus.getDefault().isRegistered(this))) {
            EventBus.getDefault().register(this);
        }
        mImgCover = (ImageView) findViewById(R.id.special_detail_activity_cover);
        mTitleTxt = (TextView) findViewById(R.id.special_detail_activity_title);
        mPlayTxt = (TextView) findViewById(R.id.special_detail_activity_playcount);
        mPrecenterTxt = (TextView) findViewById(R.id.special_detail_activity_pricenter);
        mClassifyTxt = (TextView) findViewById(R.id.special_detail_activity_class);

        mRefresh = (SwipeRefreshLayout) findViewById(R.id.special_detail_activity_refresh);
        if (mRefresh != null) {
            mRefresh.setOnRefreshListener(this);
            //设置刷新时动画的颜色，可以设置4个
            mRefresh.setColorSchemeResources(
                    android.R.color.holo_blue_light,
                    android.R.color.holo_red_light,
                    android.R.color.holo_orange_light,
                    android.R.color.holo_green_light
            );
        }

        Intent intent = getIntent();
        special = intent.getParcelableExtra("special");

        ClientAPI.getSpecialDetail(special.getAlbumId(),1,20);

        Toolbar mToolbar= (Toolbar) findViewById(R.id.special_detail_activity_toolbar);

        setSupportActionBar(mToolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setTitle("专辑详情");
        }

        mList = new ArrayList<>();
        mListView = (ListView) findViewById(R.id.special_detail_activity_listview);
        trackAdapter = new TrackAdapter(mList, this);
        mListView.setAdapter(trackAdapter);

        mListView.setOnScrollListener(this);
        mListView.setOnItemClickListener(this);


    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onGetSpecialDetail(SpecialDetail specialDetail){
        Album album = specialDetail.getAlbum();
        List<Track> tracks = specialDetail.getTracks();

        mTitleTxt.setText(album.getTitle());
        mPrecenterTxt.setText("主播： "+album.getNickname());
        mPlayTxt.setText("播放： "+String.valueOf(album.getPlayTimes()));
        mClassifyTxt.setText("分类： "+album.getCategoryName());

        Picasso.with(this)
                .load(album.getCoverMiddle())
                .config(Bitmap.Config.ARGB_8888)
                .resize(250,250)
                .into(mImgCover);

        //Toast.makeText(this, album.toString(), Toast.LENGTH_SHORT).show();

        trackAdapter.addAll(tracks);
        mRefresh.setRefreshing(false);
    }


    /**
     * 手机状态栏透明化:代码实现（还可以在style中设置）
     *
     *
     */
    private void translucentStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0及以上
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4到5.0
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    public void onRefresh() {
        ClientAPI.getSpecialDetail(special.getAlbumId(),1,20);
    }


    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        switch (scrollState) {
            case SCROLL_STATE_IDLE:// 滑动停止时
                if (view.getLastVisiblePosition()==(view.getCount()-1)) {
                    Toast.makeText(this, "上拉加载", Toast.LENGTH_SHORT).show();
                }
                break;
            case SCROLL_STATE_FLING://快速滑动时
                break;
            case SCROLL_STATE_TOUCH_SCROLL:

                break;
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this, "点击了", Toast.LENGTH_SHORT).show();
//
//        Uri uri = Uri.parse("http://live.xmcdn.com/192.168.3.136/live/93/24/161103_154218_0.aac");
//        //  http://fdfs.xmcdn.com/group5/M06/C8/9C/wKgDtlSDMWWxO3jvAMva0PmUfX0357.mp3
//        //	http://live.xmcdn.com/192.168.3.136/live/93/24/161103_154218_0.aac
//        //  http://live.xmcdn.com/live/93/24.m3u8
//
//        MediaPlayer mPlayer=MediaPlayer.create(this,uri);
//
//        mPlayer.start();


        Track track = mList.get(position);
        long trackId = track.getTrackId();
        long uid = track.getUid();

        Intent intent = new Intent(this, PlayActivity.class);
        intent.putExtra("uid" , uid);
        intent.putExtra("trackId" , trackId);
        startActivity(intent);


    }
}
